home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / galaga.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  9KB  |  368 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12.  
  13.  
  14. #define MAX_STARS 250
  15. #define STARS_COLOR_BASE 32
  16.  
  17. unsigned char *galaga_starcontrol;
  18. static unsigned int stars_scroll;
  19. static int flipscreen;
  20.  
  21. struct star
  22. {
  23.     int x,y,col,set;
  24. };
  25. static struct star stars[MAX_STARS];
  26. static int total_stars;
  27.  
  28.  
  29.  
  30. /***************************************************************************
  31.  
  32.   Convert the color PROMs into a more useable format.
  33.  
  34.   Galaga has one 32x8 palette PROM and two 256x4 color lookup table PROMs
  35.   (one for characters, one for sprites). Only the first 128 bytes of the
  36.   lookup tables seem to be used.
  37.   The palette PROM is connected to the RGB output this way:
  38.  
  39.   bit 7 -- 220 ohm resistor  -- BLUE
  40.         -- 470 ohm resistor  -- BLUE
  41.         -- 220 ohm resistor  -- GREEN
  42.         -- 470 ohm resistor  -- GREEN
  43.         -- 1  kohm resistor  -- GREEN
  44.         -- 220 ohm resistor  -- RED
  45.         -- 470 ohm resistor  -- RED
  46.   bit 0 -- 1  kohm resistor  -- RED
  47.  
  48. ***************************************************************************/
  49. void galaga_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  50. {
  51.     int i;
  52.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  53.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  54.  
  55.  
  56.     for (i = 0;i < 32;i++)
  57.     {
  58.         int bit0,bit1,bit2;
  59.  
  60.  
  61.         bit0 = (color_prom[31-i] >> 0) & 0x01;
  62.         bit1 = (color_prom[31-i] >> 1) & 0x01;
  63.         bit2 = (color_prom[31-i] >> 2) & 0x01;
  64.         palette[3*i] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  65.         bit0 = (color_prom[31-i] >> 3) & 0x01;
  66.         bit1 = (color_prom[31-i] >> 4) & 0x01;
  67.         bit2 = (color_prom[31-i] >> 5) & 0x01;
  68.         palette[3*i + 1] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  69.         bit0 = 0;
  70.         bit1 = (color_prom[31-i] >> 6) & 0x01;
  71.         bit2 = (color_prom[31-i] >> 7) & 0x01;
  72.         palette[3*i + 2] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  73.     }
  74.  
  75.     color_prom += 32;
  76.  
  77.     /* characters */
  78.     for (i = 0;i < TOTAL_COLORS(0);i++)
  79.         COLOR(0,i) = 15 - (*(color_prom++) & 0x0f);
  80.  
  81.     color_prom += 128;
  82.  
  83.     /* sprites */
  84.     for (i = 0;i < TOTAL_COLORS(1);i++)
  85.     {
  86.         if (i % 4 == 0) COLOR(1,i) = 0;    /* preserve transparency */
  87.         else COLOR(1,i) = 15 - ((*color_prom & 0x0f)) + 0x10;
  88.  
  89.         color_prom++;
  90.     }
  91.  
  92.     color_prom += 128;
  93.  
  94.  
  95.     /* now the stars */
  96.     for (i = 32;i < 32 + 64;i++)
  97.     {
  98.         int bits;
  99.         int map[4] = { 0x00, 0x88, 0xcc, 0xff };
  100.  
  101.         bits = ((i-32) >> 0) & 0x03;
  102.         palette[3*i] = map[bits];
  103.         bits = ((i-32) >> 2) & 0x03;
  104.         palette[3*i + 1] = map[bits];
  105.         bits = ((i-32) >> 4) & 0x03;
  106.         palette[3*i + 2] = map[bits];
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. /***************************************************************************
  113.  
  114.   Start the video hardware emulation.
  115.  
  116. ***************************************************************************/
  117. int galaga_vh_start(void)
  118. {
  119.     int generator;
  120.     int x,y;
  121.     int set = 0;
  122.  
  123.  
  124.     if (generic_vh_start() != 0)
  125.         return 1;
  126.  
  127.  
  128.     /* precalculate the star background */
  129.     /* this comes from the Galaxian hardware, Galaga is probably different */
  130.     total_stars = 0;
  131.     generator = 0;
  132.  
  133.     for (y = 0;y <= 255;y++)
  134.     {
  135.         for (x = 511;x >= 0;x--)
  136.         {
  137.             int bit1,bit2;
  138.  
  139.  
  140.             generator <<= 1;
  141.             bit1 = (~generator >> 17) & 1;
  142.             bit2 = (generator >> 5) & 1;
  143.  
  144.             if (bit1 ^ bit2) generator |= 1;
  145.  
  146.             if (((~generator >> 16) & 1) && (generator & 0xff) == 0xff)
  147.             {
  148.                 int color;
  149.  
  150.                 color = (~(generator >> 8)) & 0x3f;
  151.                 if (color && total_stars < MAX_STARS)
  152.                 {
  153.                     stars[total_stars].x = x;
  154.                     stars[total_stars].y = y;
  155.                     stars[total_stars].col = Machine->pens[color + STARS_COLOR_BASE];
  156.                     stars[total_stars].set = set;
  157.                     if (++set > 3)
  158.                         set = 0;
  159.  
  160.                     total_stars++;
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     return 0;
  167. }
  168.  
  169.  
  170.  
  171. WRITE_HANDLER( galaga_flipscreen_w )
  172. {
  173.     if (flipscreen != (data & 1))
  174.     {
  175.         flipscreen = data & 1;
  176.         memset(dirtybuffer,1,videoram_size);
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. /***************************************************************************
  183.  
  184.   Draw the game screen in the given osd_bitmap.
  185.   Do NOT call osd_update_display() from this function, it will be called by
  186.   the main emulation engine.
  187.  
  188. ***************************************************************************/
  189. void galaga_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  190. {
  191.     int offs;
  192.  
  193.  
  194.     /* for every character in the Video RAM, check if it has been modified */
  195.     /* since last time and update it accordingly. */
  196.     for (offs = videoram_size - 1;offs >= 0;offs--)
  197.     {
  198.         if (dirtybuffer[offs])
  199.         {
  200.             int sx,sy,mx,my;
  201.  
  202.  
  203.             dirtybuffer[offs] = 0;
  204.  
  205.         /* Even if Galaga's screen is 28x36, the memory layout is 32x32. We therefore */
  206.         /* have to convert the memory coordinates into screen coordinates. */
  207.         /* Note that 32*32 = 1024, while 28*36 = 1008: therefore 16 bytes of Video RAM */
  208.         /* don't map to a screen position. We don't check that here, however: range */
  209.         /* checking is performed by drawgfx(). */
  210.  
  211.             mx = offs % 32;
  212.             my = offs / 32;
  213.  
  214.             if (my <= 1)
  215.             {
  216.                 sx = my + 34;
  217.                 sy = mx - 2;
  218.             }
  219.             else if (my >= 30)
  220.             {
  221.                 sx = my - 30;
  222.                 sy = mx - 2;
  223.             }
  224.             else
  225.             {
  226.                 sx = mx + 2;
  227.                 sy = my - 2;
  228.             }
  229.  
  230.             if (flipscreen)
  231.             {
  232.                 sx = 35 - sx;
  233.                 sy = 27 - sy;
  234.             }
  235.  
  236.             drawgfx(tmpbitmap,Machine->gfx[0],
  237.                     videoram[offs],
  238.                     colorram[offs],
  239.                     flipscreen,flipscreen,
  240.                     8*sx,8*sy,
  241.                     &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  242.         }
  243.     }
  244.  
  245.  
  246.     /* copy the character mapped graphics */
  247.     copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  248.  
  249.  
  250.     /* Draw the sprites. */
  251.     for (offs = 0;offs < spriteram_size;offs += 2)
  252.     {
  253.         if ((spriteram_3[offs + 1] & 2) == 0)
  254.         {
  255.             int code,color,flipx,flipy,sx,sy,sfa,sfb;
  256.  
  257.  
  258.             code = spriteram[offs];
  259.             color = spriteram[offs + 1];
  260.             flipx = spriteram_3[offs] & 1;
  261.             flipy = spriteram_3[offs] & 2;
  262.             sx = spriteram_2[offs + 1] - 40 + 0x100*(spriteram_3[offs + 1] & 1);
  263.             sy = 28*8 - spriteram_2[offs];
  264.             sfa = 0;
  265.             sfb = 16;
  266.  
  267.             /* this constraint fell out of the old, pre-rotated code automatically */
  268.             /* we need to explicitly add it because of the way we flip Y */
  269.             if (sy <= -16)
  270.                 continue;
  271.  
  272.             if (flipscreen)
  273.             {
  274.                 flipx = !flipx;
  275.                 flipy = !flipy;
  276.                 sfa = 16;
  277.                 sfb = 0;
  278.             }
  279.  
  280.             if ((spriteram_3[offs] & 0x0c) == 0x0c)        /* double width, double height */
  281.             {
  282.                 drawgfx(bitmap,Machine->gfx[1],
  283.                         code+2,color,flipx,flipy,sx+sfa,sy-sfa,
  284.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  285.                 drawgfx(bitmap,Machine->gfx[1],
  286.                         code,color,flipx,flipy,sx+sfa,sy-sfb,
  287.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  288.  
  289.                 drawgfx(bitmap,Machine->gfx[1],
  290.                         code+3,color,flipx,flipy,sx+sfb,sy-sfa,
  291.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  292.                 drawgfx(bitmap,Machine->gfx[1],
  293.                         code+1,color,flipx,flipy,sx+sfb,sy-sfb,
  294.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  295.             }
  296.             else if (spriteram_3[offs] & 8)    /* double width */
  297.             {
  298.                 drawgfx(bitmap,Machine->gfx[1],
  299.                         code+2,color,flipx,flipy,sx,sy-sfa,
  300.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  301.                 drawgfx(bitmap,Machine->gfx[1],
  302.                         code,color,flipx,flipy,sx,sy-sfb,
  303.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  304.             }
  305.             else if (spriteram_3[offs] & 4)    /* double height */
  306.             {
  307.                 drawgfx(bitmap,Machine->gfx[1],
  308.                         code,color,flipx,flipy,sx+sfa,sy,
  309.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  310.                 drawgfx(bitmap,Machine->gfx[1],
  311.                         code+1,color,flipx,flipy,sx+sfb,sy,
  312.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  313.             }
  314.             else    /* normal */
  315.                 drawgfx(bitmap,Machine->gfx[1],
  316.                         code,color,flipx,flipy,sx,sy,
  317.                         &Machine->drv->visible_area,TRANSPARENCY_THROUGH,Machine->pens[0]);
  318.         }
  319.     }
  320.  
  321.  
  322.     /* draw the stars */
  323.     if (galaga_starcontrol[5] & 1)
  324.     {
  325.         int bpen;
  326.  
  327.  
  328.         bpen = Machine->pens[0];
  329.         for (offs = 0;offs < total_stars;offs++)
  330.         {
  331.             int x,y;
  332.             int set;
  333.             int starset[4][2] = {{0,3},{0,1},{2,3},{2,1}};
  334.  
  335.             set = ((galaga_starcontrol[4] << 1) | galaga_starcontrol[3]) & 3;
  336.             if ((stars[offs].set == starset[set][0]) ||
  337.                 (stars[offs].set == starset[set][1]))
  338.             {
  339.                 x = ((stars[offs].x + stars_scroll) % 512) / 2 + 16;
  340.                 y = (stars[offs].y + (stars_scroll + stars[offs].x) / 512) % 256;
  341.  
  342.                 if (y >= Machine->drv->visible_area.min_y &&
  343.                     y <= Machine->drv->visible_area.max_y)
  344.                 {
  345.                     if (read_pixel(bitmap, x, y) == bpen)
  346.                         plot_pixel(bitmap, x, y, stars[offs].col);
  347.                 }
  348.             }
  349.         }
  350.     }
  351. }
  352.  
  353.  
  354.  
  355. void galaga_vh_interrupt(void)
  356. {
  357.     /* this function is called by galaga_interrupt_1() */
  358.     int s0,s1,s2;
  359.     int speeds[8] = { 2, 3, 4, 0, -4, -3, -2, 0 };
  360.  
  361.  
  362.     s0 = galaga_starcontrol[0] & 1;
  363.     s1 = galaga_starcontrol[1] & 1;
  364.     s2 = galaga_starcontrol[2] & 1;
  365.  
  366.     stars_scroll -= speeds[s0 + s1*2 + s2*4];
  367. }
  368.